package jcommon.chap9_templating.velocity.simple;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.velocity.VelocityContext;

import jcommon.chap9_templating.velocity.demoframework.DefautEngineDemo;
import jcommon.chap9_templating.velocity.simple.bean.Customer;
import jcommon.chap9_templating.velocity.simple.bean.Magazine;
import jcommon.chap9_templating.velocity.simple.bean.Subscription;

public class SimpleDemo extends DefautEngineDemo {

	@Override
	public Reader getTemplateReader() {
		try {
			return  new FileReader(getClass().getResource("renew.vm").getFile());
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public VelocityContext getVelocityContext() {
		VelocityContext context = new VelocityContext();
		context.put("subscription", getTestSubscription( ));
		return context;
	}

	private Subscription getTestSubscription() {
		Customer c = new Customer();
		c.setId(RandomStringUtils.randomAlphabetic(12));
		c.setFirstName("Ke");
		Magazine m = new Magazine();
		m.setTitle("时代周刊");
		m.setBaseUrl("www.dota.com");
		Subscription subscription = new Subscription();
		subscription.setCustomer(c);
		subscription.setMagazine(m);
		subscription.setEndDate(new Date());
		return subscription;
	}

}
